//-----------------先初始化裁剪区------------------

let images = $('#images') //找到图片

let option = { //设置宽高比和预览区
    aspectRatio: 1,
    preview: '.preview-size',
}

images.cropper(option) //调用cropper方法 创建裁剪区

// --------点击文件域按钮切换图片---------
$('#file').on('change', function() {
    let file = this.files
    if (file.length > 0) {
        let url = URL.createObjectURL(file[0])
        console.log(url);

        $('#images').cropper('destroy').attr('src', url).cropper(option)
    }

})

//----------- 点击确定按钮修改图片------------- 
$('#sure').on('click', function(e) {
    e.preventDefault()
    let canvas = images.cropper('getCroppedCanvas', { width: 30, height: 30 })
    let base64 = canvas.toDataURL()
        // console.log(base64); //base64格式
    $.ajax({
        url: '/my/user/avatar',
        type: 'POST',
        data: { avatar: base64 },
        success: function(res) {
            // console.log(res);
            if (res.status === 0) {
                layer.msg(res.message)
                window.parent.rendererIndex()
            }
        }
    })

})